The method overloading allows creating several methods with the same name, which differ from each other in the type of the input of the method. It is simply defined as the ability of one method to perform different tasks. Compiler identifies which of the overloaded methods to execute based on a number of arguments and their data types during compilation itself.
Pseudo code is used in all examples provided in this work. Pseudo code is based on the general purpose language C# [2] including implicit parameter and argument extension features.
The method overloading examples are shown in FIG. 1. When designing complex programs, the code changes and some branches may become unnecessary as shown in FIG. 2. In such cases overloaded methods can improve code readability as shown in FIG. 3., because they provide more static information during the compile-time and they typically result in a better runtime performance.
This works fine for a single parameter, but some problems might occur, when there are multiple options. Each extra option doubles the number of possible overloads; and if two of them are of the same type, problems can arise due to trying to declare multiple methods with the same signature. Number of possible overloads could be significantly reduced with optional arguments1. [1, 2, 3] 1 Sometimes arguments are called: actual parameters.
Optional arguments are usually used when there are several values required for an operation, where the same values are used multiple times. Optional argument declaration example is shown in FIG. 4, method calling examples are provided in FIG. 5, which produces output shown in FIG. 6.
When supplying unnamed arguments, their order should match the order of parameters in the method declaration, it is not possible to supply first and third argument omitting second argument. The code shown in FIG. 7. is invalid. This problem can be solved by using named arguments. [3]
The basic idea of named arguments is that specifying an argument value it is also possible to specify the name of the parameter, which will receive value of specified argument. The compiler then makes sure that there is a parameter of the right name and uses the value for that parameter. Even on its own, this can increase readability in some cases. In reality, named arguments are the most useful in cases, where optional arguments are also likely to appear.
An example with supplying first and third arguments to method DoSomething1 (FIG. 4) call is the following:
DoSomething1 (3, optionalInteger: 3);
Method call produces result shown in FIG. 8.
The named arguments and optional arguments affect how the compiler resolves overloads. Optional arguments can increase the number of applicable methods (if some methods have more parameters2 than the number of specified arguments) and named arguments can decrease the number of applicable methods (by ruling out methods that do not have the appropriate parameter names). 2 Sometimes parameters are called: formal parameters.
The parameter names of a method are effectively part of the API. If you change them at a later date, code can break—anything that was using a named argument to refer to one of your parameters will fail to compile if you decide to change it.
All previously reviewed methods are methods with the parameters and arguments passed by value. If a parameter is declared for a method without ref or out, the parameter can have a value associated with it. That value can be changed in the method, but the changed value will not be retained, when control passes back to the calling procedure. [2]
The ref keyword causes an argument to be passed by reference, not by value. The effect of passing by reference is that any change to the parameter in the method is reflected in the underlying argument variable in the calling method. The value of a reference parameter is always the same as the value of the underlying argument variable. [2]
A parameter declared with an ‘out’ modifier is an output parameter. Similar to a reference parameter, an output parameter does not create a new storage location. Instead, an output parameter represents the same storage location as the variable given as the argument in the method invocation. A variable does not need to be definitely assigned before it can be passed as an output parameter, but following an invocation where a variable was passed as an output parameter, the variable is considered definitely assigned. [2]
In FIG. 9. examples of parameter passed by value, parameter passed by reference and output parameter are demonstrated. A reference and output parameter cannot have a default value, so these parameters cannot be optional. The required arguments cannot be omitted in method calls.
The Scala language specification [4] (current version 2.8) supports the feature called “implicit parameters”3, which is similar to optional arguments. Scala implicit parameters allow declaring the last parameter list of a function to be implicit. The syntax for this works as demonstrated in FIG. 10. (using Scala REPL4). 3 Which in this paper are called “scala implicit parameters”, because they are declared outside of the method declaration. Historically idea of parameters is related to a method. If parameters are declared outside of method, then they are not parameters of method by definition, they are variables of scope higher than scope of method instead.4 Read-eval-print loop.
It is possible to call speakImplicitly as normal but, additionally, we can leave out the implicit argument list and the compiler will look for a value in the enclosing scope, which has been marked as implicit. If we try to do that and there is no such value in scope, then the compiler will return error code.
Implicits are type safe and they are selected based on the static type of the arguments. This means that Scala implicit parameters allow implicit parameter reuse in the different functions. However, if there are multiple Scala implicit parameters of the same type, compiler will fail to use any, because it is not able to choose between them as demonstrated in FIG. 11.
All identified problems and limitations of the C# optional arguments and Scala implicit parameters can be solved with the implicit parameters and implicit arguments.